package cn.fyl.org.controller;

import cn.fyl.basic.util.JsonResult;
import cn.fyl.basic.util.PageList;
import cn.fyl.org.domain.Employee;
import cn.fyl.org.query.EmployeeQuery;
import cn.fyl.org.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/emp")
public class EmployeeController {

    @Autowired
    private IEmployeeService EmployeeService;

    /**
     * 查询所有
     * @return
     */
    @GetMapping
    public List<Employee> findAll(){
        return EmployeeService.loadAll();
    }

    /**
     * 查询一个
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Employee findById(@PathVariable("id") Long id){
        return EmployeeService.loadById(id);
    }

    /**
     * 删除操作
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public JsonResult delete(@PathVariable("id") Long id){
        try {
            EmployeeService.remove(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }

    /**
     * 添加和修改操作
     * @param Employee
     * @return
     */
    @PostMapping
    public JsonResult save(@RequestBody Employee Employee){
        try {
            if(Employee.getId()==null){
                EmployeeService.save(Employee);
            }else {
                EmployeeService.update(Employee);
            }
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }

    }

    /**
     * 分页查询
     * @param
     * @return
     */
    @PutMapping
    public PageList<Employee> queryPage(@RequestBody EmployeeQuery query){
        try {
            return EmployeeService.queryData(query);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<Employee>();
        }
    }

    @PatchMapping
    public JsonResult patchDelete(@RequestBody List<Long> ids){
        try {
            EmployeeService.patchDelete(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }

    @GetMapping("/list/{id}")
    public List<Employee> findByShopId(@PathVariable("id") Long id){
        return EmployeeService.findByShopId(id);
    }
}
